//
//  DataBaseHandle.m
//  UI21_豆瓣
//
//  Created by dllo on 15/10/15.
//  Copyright (c) 2015年 Dior_3g. All rights reserved.
//

#import "DataBaseHandle.h"

@implementation DataBaseHandle

#pragma mark 单例方法
+ (instancetype)shareDataBaseHandle {
    
    static DataBaseHandle *handle;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        handle = [[DataBaseHandle alloc] init];
        
    });
    
//    [handle openDB];
    return handle;
    
}



#pragma mark 开数据库
- (void)openDB {
    
    //      1. 沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //      2. 拼接路径
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"model.sqlite"];
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
    NSLog(@"%@", dbPath);
    
}

#pragma mark 建表
- (void)createTable {
    
//    NSString *sqlStr = @"create table if not exists movie(movieId text primary key , movieName text, pic_url text)";
    
    
    NSString *sqlStr = @"create table if not exists model(pic text, title text, likes text,tableId text, flag integer)";
    
    
    //      执行sql语句
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    } else {
        NSLog(@"创建表失败");
    }
}

#pragma mark 查询
- (SelectModel)isHaveInTable:(HomePageTableViewModel *)model {
    
    //      除了openDB, 都先写SQL语句
//    NSString *sqlStr = [NSString stringWithFormat:@"select * from movie where movieId = '%@'", movie.movieId];
    
    
     NSString *sqlStr = [NSString stringWithFormat:@"select * from model where tableId = '%@' and flag = '1'", model.tableId];
    

    
    //      跟随指针
    sqlite3_stmt *stmt = nil;
    
    //      执行SQL语句
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            sqlite3_finalize(stmt);
            return InTable;
        } else {
            
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    } else {
        
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
        return SelectError;
    }
    
    return YES;
}

#pragma mark 收藏(添加)
- (void)saveModelInDB:(HomePageTableViewModel *)model {
    
//    NSString *sqlStr = [NSString stringWithFormat:@"insert into movie(movieId, movieName, pic_url) values ('%@', '%@', '%@')", movie.movieId, movie.movieName, movie.pic_url];
    
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into model(pic, title, likes, tableId, flag) values ('%@', '%@', '%@', '%@', '1')", model.pic, model.title, model.likes, model.tableId];
    
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
    
}
 


#pragma mark 取消收藏, 直接删除

- (void)deleteModelInDB:(HomePageTableViewModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from model where tableId ='%@'", model.tableId];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}


/*
#pragma mark 取消收藏

- (void)deleteMovieInDB:(Suger *)suger {
    
    NSString *sqlStr = [NSString stringWithFormat:@"update suger set flag = '0' where playId = '%@'", suger.playId];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}
*/

- (NSMutableArray *)selectAllModel {
    
    NSMutableArray *modelArray = [NSMutableArray array];
    
    NSString *sqlStr = @"select * from model where flag = '1'";
    
    //      跟随指针
    sqlite3_stmt *stmt = nil;
    
    //      执行查找
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            HomePageTableViewModel *model = [[HomePageTableViewModel alloc] init];
            
            model.pic = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            model.title = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            model.likes = @(sqlite3_column_int(stmt, 2));
            model.tableId = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            [modelArray addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return modelArray;
    
}







@end
